package com.telecom.util;

import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.List;

public class EntityFactory {
    public static <T> T getBean(Class<T> tClass) {
       try{
           SAXReader reader = new SAXReader();
           InputStream inputStream=EntityFactory.class.getClassLoader().getResourceAsStream("bean.xml");
           Document doc = reader.read(inputStream);
           Element rootElement = doc.getRootElement();
           List<Element> list = rootElement.elements("bean");
           for (Element element : list) {
               String s = element.attributeValue("class");
               if (s.equals(tClass.getName())){
                   //创建对象的实例
                   T t = tClass.getDeclaredConstructor().newInstance();
                   //获取属性
                   List<Element> properties = element.elements("property");
                   for (Element property : properties) {
                       String name = property.attributeValue("name");//name
                       String value = property.attributeValue("value");//张三
                       //获取实体类对应的属性，用value值给属性赋值
                       //setName   name
                       Field  field = tClass.getDeclaredField(name);
                       field.setAccessible(true);//设置私有属性可访问
                       //根据数据类型，做转换处理
                       BeanUtils.setProperty(t,name,value);
                      /* Class<?> type = field.getType();
                       if (type.equals(Integer.class)){
                           field.set(t,Integer.valueOf(value));
                       }
                       if (type.equals(String.class)){
                           field.set(t,value);
                       }
                       if (type.equals(Double.class)){
                           field.set(t,Double.valueOf(value));
                       }
                       if (type.equals(Float.class)){
                           field.set(t,Float.valueOf(value));
                       }
                       if (type.equals(Long.class)){
                           field.set(t,Long.valueOf(value));
                       }
                       if (type.equals(Boolean.class)){
                           field.set(t,Boolean.valueOf(value));
                       }
                       if (type.equals(Character.class)){
                           field.set(t,value.charAt(0));
                       }
                       if (type.equals(Byte.class)){
                           field.set(t,Byte.valueOf(value));
                       }
                       if (type.equals(Short.class)){
                           field.set(t,Short.valueOf(value));
                       }*/
                       //field.set(t,value);
                   }
                   return t;
               }
           }
       }catch (Exception e){
           throw new RuntimeException("创建对象失败:"+e);
       }
       return null;
    }
}
